package com.iwords.android;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.View;
import android.webkit.WebView;

public class ShowNotify extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.notify);
		//showNotify();//从服务器端获取通知信息，可能时message，也可能是url，分情况显示。
		setContentView(R.layout.notify);
		
		Intent intent = getIntent();
		//System.out.println("ShowNotify.onCreate() intent.hasExtra(\"messageid\") = " + intent.hasExtra("messageid"));
		//System.out.println("ShowNotify.onCreate() intent.hasExtra(\"messagetype\") = " + intent.hasExtra("messagetype"));
		//System.out.println("ShowNotify.onCreate() intent.hasExtra(\"message\") = " + intent.hasExtra("message"));
		//System.out.println("ShowNotify.onCreate() intent.hasExtra(\"currentlib\") = " + intent.hasExtra("currentlib"));
		//System.out.println("ShowNotify.onCreate() intent.getIntExtra(\"messageid\") = " + intent.getIntExtra("messageid", -1));
		//System.out.println("ShowNotify.onCreate() intent.getIntExtra(\"messagetype\") = " + intent.getIntExtra("messagetype", -1));
		//System.out.println("ShowNotify.onCreate() intent.getLongExtra(\"currentlib\") = " + intent.getLongExtra("currentlib", -1));
		//System.out.println("ShowNotify.onCreate() intent.getStingExtra(\"message\") = " + intent.getStringExtra("message"));
		int messageType = intent.getIntExtra("messagetype", -1);
		String message = intent.getStringExtra("message");
		if(1 == messageType){
			showURL(message);
		}else{
			showText(message);
		}
	}
	
	private void showURL(String url){
		WebView webView = (WebView)this.findViewById(R.id.notify_web);
		webView.loadUrl(CodeUtils.decode(url));
	}
	
	private void showText(String message){
		WebView webView = (WebView)this.findViewById(R.id.notify_web);
		webView.loadData(CodeUtils.decode(message), "text/plain",  "utf-8");
	}
	private void vibrate(){
		Vibrator mVibrator = (Vibrator)getApplication().getSystemService(android.app.Service.VIBRATOR_SERVICE);
		mVibrator.vibrate(new long[]{50,50},-1);
	}
	/**
	 * 用户可以选择关闭，或不再显示。
	 * 关闭：下次仍然显示该信息
	 * 不再显示：只有有了新信息才会显示 
	 */
	public void close(View view) {//系统通知信息关闭时，要么显示词库选择，要么进入背单词
		this.vibrate();
		IWordsApp iWordsApp = (IWordsApp) getApplicationContext();
		long userid = iWordsApp.getUser().getUserid();
		DBHelper dbHelper = new DBHelper(this);
		try{
			DBHelper.UserLibs userLib = dbHelper.getCurrentLib(userid);
			if (userLib!=null) {
				// 如果用户已经选过词库，则跳转到记单词页面
				Intent intent = new Intent(this, ProgressingActivity.class);
				intent.putExtra("currentLibId", userLib.libid);
				this.startActivity(intent);
			} else {
				// System.out.println("登录成功，用户没有当前课本，跳转到课本选择页");
				// 如果用户没有选过词库，则跳转到选择词库界面
				Intent intent = new Intent(this, ShowWordLib.class);
				this.startActivityForResult(intent, PICK_WORD_LIB_REQUEST);
			}
		}finally{
			dbHelper.cleanup();
		}
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				ShowNotify.this.finish();
			}

		}, WAIT_LENGHT);
	}
	private final int WAIT_LENGHT = 2000; // 延迟三秒
	final static int PICK_WORD_LIB_REQUEST = 0;
	/**
	 * 选择新课本，则意味着将所选对应课本的单词初始化到 用户的单词信息中，然后才进入背单词页面
	 */
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		IWordsApp iWordsApp = (IWordsApp) getApplicationContext();
		if (requestCode == PICK_WORD_LIB_REQUEST) {
			if (resultCode == RESULT_OK) {
				// System.out.println("IWords.onActivityResult() data = " +
				// data);
				Long libId = data.getLongExtra("selectedLibId", 1L);
				Boolean isLeaf = data.getBooleanExtra("isLeaf", Boolean.FALSE);
				if (isLeaf) {
					Intent intent = new Intent(this, DownloadWordsLib.class);
					intent.putExtra("currentWordLibId", libId);
					this.startActivity(intent);
//					data.putExtra("currentWordLibId", libId);
//					if (new ChangeWordLib(iWordsApp, this).userSelectedWordLib(libId)) {
//						showWord(libId);
//					}
				} else {
					Intent intent = new Intent(this, ShowWordLib.class);
					intent.putExtras(data);
					this.startActivityForResult(intent, PICK_WORD_LIB_REQUEST);
				}
			}
		}
	}
	private void showWord(Long libId) {
		// 如果用户已经选过词库，则跳转到记单词页面
		Intent intent = new Intent(this, ProgressingActivity.class);
		intent.putExtra("currentLibId", libId);
		this.startActivity(intent);
	}

	public void neverShowAgain(View view) {
		this.vibrate();
		neverShowAgain();
		close(view);
	}

	private void neverShowAgain() {
		IWordsApp iWordsApp = (IWordsApp) getApplicationContext();
		SharedPreferences settings = this.getSharedPreferences(ShowNotify.class.getName(), MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(iWordsApp.getUser().getUserid()+"_showmessage_"+getIntent().getIntExtra("messageid", -1), false);
		editor.commit();
	}
	public void onResume() {
		super.onResume(); 
		com.mobclick.android.MobclickAgent.onResume(this);
	}
	
	public void onPause() {
		super.onPause(); 
		com.mobclick.android.MobclickAgent.onPause(this);
	}

}
